package org.wmq.transferordersystem.contrller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.transferordersystem.model.Entity.Order;
import org.wmq.transferordersystem.model.Entity.Result;
import org.wmq.transferordersystem.service.OrderService;

import java.util.List;

@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {
    private final OrderService orderService;

    @PostMapping
    public Result<Void> saveOrder(@RequestBody Order order){
        System.out.println("controller:  " + order);
        this.orderService.save(order);
        return Result.ok();
    }

    @DeleteMapping("/delete/{id}")
    public Result<Void> deleteOrder(@PathVariable Long id){
        boolean success = this.orderService.deleteById(id);
        if (success) {
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    @GetMapping("/{id}")
    public Result<Order> getOrderById(@PathVariable Long id){
        Order order = this.orderService.getById(id);
        if (order == null) {
            return Result.error("订单不存在");
        }
        return Result.ok(order);
    }

    @GetMapping("/list")
    public Result<List<Order>> getOrderList(){
        List<Order> result = this.orderService.list();
        if (result == null || result.isEmpty()){
            return Result.error("订单不存在");
        }
        return Result.ok(result);
    }
}
